package com.wanmait.zaisousuo.webcontroller;


import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.pojo.Article;
import com.wanmait.zaisousuo.pojo.ArticleComment;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.ArticleCommentService;
import com.wanmait.zaisousuo.service.ArticleService;
import com.wanmait.zaisousuo.service.GarbageTypeService;
import com.wanmait.zaisousuo.service.TextureService;

import com.wanmait.zaisousuo.util.Pager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/article")
public class ArticleWebController {



    @Resource()
    private GarbageTypeService garbageTypeService;

    @Resource
    private TextureService textureService;

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleCommentService articleCommentService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    //去往写文章页面
    @RequestMapping("articleAdd")
    public void articleAdd(){}

    //去往文章列表页面,转发垃圾类型和垃圾材质
    @RequestMapping("articleList")
    public ModelAndView toArticleListPage(){

        ModelAndView mv = new ModelAndView();
        mv.addObject("garbageTypes",garbageTypeService.findAllType());
        mv.addObject("textures",textureService.findAll());
        return mv;
    }

    //ajax：到articleListData页面
    @RequestMapping("articleListData")
    public ModelAndView ArticleListData(Integer typeId,Integer textId,String orderBy,Integer pageNum){
        if(pageNum==null)
        {
            pageNum=1;
        }
        ModelAndView mv = new ModelAndView();
        PageInfo<Article> pageInfo = articleService.findAllByConditions(typeId,textId,orderBy,pageNum);
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    //1.根据文章id查询出文章详情
    //2.判断此用户是否点赞过此文章，返回布尔值
    //3.判断此用户是否收藏过此文章，返回布尔值
    @RequestMapping("details")
    public ModelAndView details(Integer id,HttpSession session)
    {
        Boolean flag=false;
        Boolean collectFlag=false;
        ModelAndView mv = new ModelAndView();
        Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
        try
        {
            Integer userinfoId = userinfo.getId();
            flag = redisTemplate.opsForSet().isMember("article:"+id+":praise:"+userinfoId+":set",userinfoId+"");
        }
        catch (NullPointerException e){}
        catch (ClassCastException e){}


        try{
            Integer userinfoId2 = userinfo.getId();
            collectFlag = redisTemplate.opsForSet().isMember("article:"+id+":collect:userinfo:set",userinfoId2+"");
        }
        catch (NullPointerException e){}
        catch (ClassCastException e){}

        mv.addObject("flag",flag);
        mv.addObject("collectFlag",collectFlag);
        mv.addObject("article",articleService.findById(id));
        return mv;
    }



    //ajax显示所有顶级父级评论
    @RequestMapping("fatherCommentData")
    public ModelAndView fatherCommentsData(Integer id,Integer pageNum){
        if(pageNum==null)
        {
            pageNum=1;
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",articleCommentService.findAllTopComments(id,pageNum));
        mv.addObject("pageNum",pageNum);
        mv.addObject("pageSize",5);
        return mv;
    }

    //根据父级id获取所有的直接子类
    @RequestMapping("childrenCommentData")
    public ModelAndView childrenCommentData(Integer id){

        ModelAndView mv = new ModelAndView();
        mv.addObject("childrenComments",articleCommentService.findAllChildrenCommentsById(id));
        return mv;
    }


    //发布文章
    @RequestMapping("toWriteArticle")
    public String toWriteArticle(HttpServletRequest request){
        Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");

        if(userinfo==null){//用户没登录
            return "redirect:/web/userinfo/login";
        }
        else{
            request.setAttribute("types",garbageTypeService.findAllType());
            request.setAttribute("textures",textureService.findAll());
            return "web/article/toWriteArticle";
        }
    }

    //提交文章
    @PostMapping("writeOneArticle")
    public String writeOneArticle(Article article){
        //文章写入数据库
        articleService.clearCache();

        articleService.addOneArticle(article);

        return "redirect:/web/article/articleList";
    }

    //文章点赞
    @GetMapping("praise")
    @ResponseBody
    public Boolean praise(Integer id,HttpSession session)
    {
        //articleService.clearCache();
        articleService.clearQuerybyidCache(id);
        Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
        Boolean flag = articleService.parise(id,userinfo.getId());
        return  flag;
    }


    //文章收藏,返回的布尔值判断是否已经收藏过
    @GetMapping("collect")
    @ResponseBody
    public Boolean collect(Integer id,String articleName,String authorName,HttpSession session){
        //articleService.clearCache();
        articleService.clearQuerybyidCache(id);
        Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
        Boolean flag =  articleService.collect(id,userinfo.getId(),articleName,authorName);
        return flag;
    }



    //提交了要搜索的字词，从solr里找
    @GetMapping("solrsearchData")
    public ModelAndView toSolrSearchData(String solrsearch,Integer pageNum) {
        if(pageNum==null)pageNum=1;
        Integer dataCount = articleService.getDataCount(solrsearch);
        Pager pager=new Pager(5,dataCount,pageNum);
        List<Article> articles = articleService.findArticlesBySolr(solrsearch,pageNum,pager);

        ModelAndView mv = new ModelAndView();
        mv.addObject("articles",articles);
        mv.addObject("pager",pager);
        return mv;
    }

    //去往solrsearch页面
    @RequestMapping("solrsearch")
    public void toSolrSearch(String solrsearch,Integer pageNum){


    }

}
